//
//  WKVoiceViewCell.m
//  WKDemo
//
//  Created by apple on 14-8-4.
//  Copyright (c) 2014年 com.app. All rights reserved.
//

#import "WKVoiceViewCell.h"
#import "DOUAudioStreamer.h"
#import "UIImageView+WebCache.h"
#import "WKVoiceModel.h"
#import "WKVoiceCellFrame.h"
#import "WKToolBar.h"
#import "WKAudioFile.h"
#import "WKStatusToolbar.h"
@interface WKVoiceViewCell()
@property(nonatomic,weak) UIImageView *iconImgView;
@property(nonatomic,weak) UILabel *nameLabel;
@property(nonatomic,weak) UILabel *timeLabel;
@property(nonatomic,weak) UILabel *contentLabel;
@property(nonatomic,weak) UILabel *playCountLabel;
@property(nonatomic,weak) UILabel *voiceLengthLabel;
@property(nonatomic,weak) UIImageView *contentImgView;
@property(nonatomic,weak) WKStatusToolbar *toolbar;
@property(nonatomic,weak) UIButton *playButton;

@property(nonatomic,strong)WKVoiceModel *voiceModel;
@property(nonatomic,strong)DOUAudioStreamer *streamer;
@property(nonatomic,strong)NSTimer *currentTimer;

@end
@implementation WKVoiceViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
  
        // 声音控制器里用到的所有控件
        //头像
        UIImageView *iconImgView=[[UIImageView alloc] init];
        [self.contentView addSubview:iconImgView];
        self.iconImgView=iconImgView;

        //昵称
        UILabel *nameLabel=[[UILabel alloc] init];
        nameLabel.font=[UIFont systemFontOfSize:12];
        [self.contentView addSubview:nameLabel];
        self.nameLabel=nameLabel;
        
        //时间
        UILabel *timeLabel=[[UILabel alloc] init];
        timeLabel.font=[UIFont systemFontOfSize:10];
        timeLabel.textColor= [UIColor orangeColor];;
        [self.contentView addSubview:timeLabel];
        self.timeLabel=timeLabel;
        
        //正文
        UILabel *contentLabel=[[UILabel alloc] init];
        contentLabel.font=[UIFont systemFontOfSize:14];
        contentLabel.textColor=[UIColor blackColor];
        contentLabel.numberOfLines= 0;
        [self.contentView addSubview:contentLabel];
        self.contentLabel=contentLabel;
        
        //图片
        UIImageView *contentImgView=[[UIImageView alloc] init];
        [self.contentView addSubview:contentImgView];
        contentImgView.userInteractionEnabled=YES;
        self.contentImgView=contentImgView;
        
        //播放次数
        UILabel *playCountLabel=[[UILabel alloc] init];
        playCountLabel.font=[UIFont systemFontOfSize:10];
        playCountLabel.textColor=[UIColor whiteColor];
        playCountLabel.backgroundColor= WKColorAlpha(0, 0, 0, 0.3);
        self.playCountLabel = playCountLabel;
        [self.contentImgView addSubview:playCountLabel];
        
        //声音时长
        UILabel *voiceLengthLabel=[[UILabel alloc] init];
        voiceLengthLabel.font=[UIFont systemFontOfSize:10];
        voiceLengthLabel.contentMode = UIViewContentModeCenter;
        voiceLengthLabel.backgroundColor=WKColorAlpha(0, 0, 0, 0.3);
        voiceLengthLabel.textColor=[UIColor whiteColor];
        self.voiceLengthLabel=voiceLengthLabel;

        [self.contentImgView addSubview:voiceLengthLabel];
    
        //播放按钮
        UIButton *playButton=[[UIButton alloc] init];
        [self.contentImgView addSubview:playButton];
        self.playButton=playButton;
        
        //工具条
        WKStatusToolbar *toolBar=[[WKStatusToolbar alloc] init];
        [self.contentView addSubview:toolBar];
        self.toolbar = toolBar;
        
    }
    return self;
}

+(instancetype)voiceCellWithTableView:(UITableView *)tableView{
    static NSString *identifer=@"voiceCell";
    
    WKVoiceViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell==nil) {
        cell=[[WKVoiceViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    return cell;
}

-(void)setCellFrame:(WKVoiceCellFrame *)cellFrame{
    _cellFrame=cellFrame;
    //设置子控件的数据和frame
    WKVoiceModel *voiceModel=cellFrame.model;
    self.voiceModel=voiceModel;
    
    self.nameLabel.text=voiceModel.name;
    self.nameLabel.frame=cellFrame.nameFrame;
    
//    [self.iconImgView setImageWithURL:[NSURL URLWithString:voiceModel.profile_image] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    
    UIImageView *imageV = [[UIImageView alloc]init];
    [imageV setImageWithURL:[NSURL URLWithString:voiceModel.profile_image] placeholderImage:[UIImage imageNamed:@"placeholderImage1"]];
    UIImage *oldImage = imageV.image;
    
    self.iconImgView.image = [UIImage circleImageWithName:oldImage borderWidth:1 borderColor:[UIColor black75PercentColor]];
    
    self.iconImgView.frame=cellFrame.iconFrame;
    
    //发表时间
    self.timeLabel.text= voiceModel.passtime;
    self.timeLabel.frame=cellFrame.timeFrame;
    
    self.contentLabel.text=voiceModel.text;
    self.contentLabel.frame=cellFrame.contentFrame;
    
    [self.contentImgView setImageWithURL:[NSURL URLWithString:voiceModel.cdn_img] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    self.contentImgView.frame=cellFrame.contentImgFrame;
    
    //设置播放按钮
    [self.playButton setBackgroundImage:[UIImage imageNamed:@"playButton"] forState:UIControlStateNormal];
    [self.playButton setImage:[UIImage imageNamed:@"playButtonPlay"] forState:UIControlStateNormal];
    
    [self.playButton setBackgroundImage:[UIImage imageNamed:@"playButtonClick"] forState:UIControlStateSelected];
    [self.playButton setImage:[UIImage imageNamed:@"playButtonPause"] forState:UIControlStateSelected];
    
    self.playButton.frame=cellFrame.playButtonFrame;
    [self.playButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //NSString *playCount=[NSString stringWithFormat:@"%@次播放",voiceModel.playcount];
    //播放次数
    
    //播放次数
    self.playCountLabel.text= [NSString stringWithFormat:@"%@次播放",voiceModel.playcount];
    self.playCountLabel.frame=cellFrame.playCountFrame;
    
    
    //音频时长的Label
    int voicetime = voiceModel.voicetime.intValue;
    NSString *time = nil;
    if (voicetime>60) {
        time = [NSString stringWithFormat:@"%0d:%02d",voicetime/60,voicetime%60];
    }else
    {
        time = [NSString stringWithFormat:@"00:%02d",voicetime];
    }
    self.voiceLengthLabel.text= time;
    self.voiceLengthLabel.frame=cellFrame.voiceLengthFrame;
    
    
    self.toolbar.frame=cellFrame.toolbarFrame;

    
    
}

-(void)addTimer{
    self.currentTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCurrentTime) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.currentTimer forMode:NSRunLoopCommonModes];
}
-(void)removeTimer{
    [self.currentTimer invalidate];
    self.currentTimer=nil;
}

-(void)btnClick:(UIButton *)btn
{
    if (btn.selected) {
        btn.selected = NO;
        self.playButton = btn;
    }else
    {
        self.playButton.selected = YES;
    }
    
    //点击按钮，播放对应的音频文件
//    NSLog(@"btnClick--%@",self.voiceModel.voiceuri);
    //    if ([self.delegate respondsToSelector:@selector(WKVoiceViewCellDidClickPalyButton:)]) {
    //        [self.delegate WKVoiceViewCellDidClickPalyButton:self];
    //    }
    //创建音频文件模型
    WKAudioFile *file=[[WKAudioFile alloc] init];
    file.audioFileURL=[NSURL URLWithString:self.voiceModel.voiceuri];
    //创建音乐播放器
    self.streamer=[DOUAudioStreamer streamerWithAudioFile:file];
    [self.streamer play];
    [self addTimer];
}

-(void)updateCurrentTime{
//    self.playSliderView.value=self.streamer.currentTime/self.streamer.duration;
//    NSLog(@"updateCurrentTime");
}

@end
